﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Data.Db;
using Data.ModelDatabase;

namespace ShopMembership
{
    public class ShopMembershipProvider : MembershipProvider
    {
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            if (string.IsNullOrEmpty(username))
                return false;

            if (string.IsNullOrEmpty(oldPassword))
                return false;

            if (string.IsNullOrEmpty(newPassword))
                return false;

            using (ShopContext contex = new ShopContext())
            {
                User user = null;
                user = contex.Users.FirstOrDefault(Usr => Usr.Username == username);
                if (user == null)
                    return false;

                if (oldPassword == user.Password)
                {
                    user.Password = newPassword;
                    contex.Commit();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            if (string.IsNullOrEmpty(username))
            {
                status = MembershipCreateStatus.InvalidUserName;
                return null;
            }
            if (string.IsNullOrEmpty(password))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }
            if (string.IsNullOrEmpty(email))
            {
                status = MembershipCreateStatus.InvalidEmail;
                return null;
            }


            using (ShopContext context = new ShopContext())
            {

                if (context.Users.Where(Usr => Usr.Username == username).Any())
                {
                    status = MembershipCreateStatus.DuplicateUserName;
                    return null;
                }

                if (context.Users.Where(Usr => Usr.Email == email).Any())
                {
                    status = MembershipCreateStatus.DuplicateEmail;
                    return null;
                }

                User newUser = new User
                {
                    Username = username,
                    Email = email,
                    Password = password,
                    Basket = new List<Basket>()
                };
                newUser.Basket.Add(new Basket() { State = State.NotOrdered });

                context.Users.Add(newUser);
                context.Commit();

                status = MembershipCreateStatus.Success;
                return new MembershipUser(Membership.Provider.Name, newUser.Username, newUser.Id.ToString(), newUser.Email, null, null, false, false, DateTime.Today, DateTime.Today, DateTime.Today, DateTime.Today, DateTime.Today);
            }
        }

        public override bool ValidateUser(string username, string password)
        {
            if (string.IsNullOrEmpty(username))
                return false;

            if (string.IsNullOrEmpty(password))
                return false;

            using (ShopContext context = new ShopContext())
            {
                User user = null;
                user = context.Users.FirstOrDefault(Usr => Usr.Username == username);
                if (user == null)
                {
                    return false;
                }
                else
                {
                    if (user.Password == password)
                        return true;
                    else
                        return false;
                }
            }
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            if (string.IsNullOrEmpty(username))
                return false;

            using (ShopContext contex = new ShopContext())
            {
                User user = null;
                user = contex.Users.FirstOrDefault(Usr => Usr.Username == username);
                if (user != null)
                {
                    contex.Users.Remove(user);
                    contex.Commit();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            if (string.IsNullOrEmpty(username))
                return null;

            using (ShopContext Context = new ShopContext())
            {
                User user = null;
                user = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
                if (user != null)
                {
                    return new MembershipUser(Membership.Provider.Name, user.Username, user.Id.ToString(), user.Email, null, null, false, false, DateTime.Today, DateTime.Today, DateTime.Today, DateTime.Today, DateTime.Today);
                }
                else
                {
                    return null;
                }
            }
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }
    }
}
